project (<%=config.projectName%>)
cmake_minimum_required(VERSION 3.5.1)
option(JS_ONLY "Compiles to native JS (No WASM)" OFF)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake-modules)

add_definitions(-std=<%=config.lang%> -O3)

# include
include_directories(include /usr/local/include)

# source entry
file(GLOB SOURCES src/*.cpp)

# output
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build")

# exec
add_executable(<%=config.libName%> ${SOURCES} ${IMG_HASH})

if(JS_ONLY)
  message(STATUS "Setting compilation target to native JavaScript")
  set(CMAKE_EXECUTABLE_SUFFIX ".js")
  set_target_properties(<%=config.libName%> PROPERTIES LINK_FLAGS "-s WASM=0 -s EXPORTED_FUNCTIONS='[_main]'")
else()
  message(STATUS "Setting compilation target to WASM")
  set(CMAKE_EXECUTABLE_SUFFIX ".wasm.js")

  message(STATUS "Setting compilation target to WASM")
  set(CMAKE_EXECUTABLE_SUFFIX ".js")
  set(LINK_FLAGS " -lembind")
  string(APPEND LINK_FLAGS " -s WASM=1")
  string(APPEND LINK_FLAGS " --no-entry")
#   string(APPEND LINK_FLAGS " -lopencv_core")
#   string(APPEND LINK_FLAGS " -lopencv_imgcodecs")
#   string(APPEND LINK_FLAGS " -lopencv_imgproc")
  string(APPEND LINK_FLAGS " -s ALLOW_MEMORY_GROWTH=1")
  string(APPEND LINK_FLAGS " -s ENVIRONMENT='web,node'")
  string(APPEND LINK_FLAGS " -s EXPORTED_FUNCTIONS=['_malloc','_free']")
  string(APPEND LINK_FLAGS " -s FORCE_FILESYSTEM=1")
  string(APPEND LINK_FLAGS " -s USE_PTHREADS=1")
  string(APPEND LINK_FLAGS " -s PTHREAD_POOL_SIZE=4")
  string(APPEND LINK_FLAGS " -s USE_LIBJPEG=1")
  string(APPEND LINK_FLAGS " -s USE_LIBPNG=1")
  string(APPEND LINK_FLAGS " -s USE_ZLIB=1")
  string(APPEND LINK_FLAGS " -s MODULARIZE=1")
  string(APPEND LINK_FLAGS " -s EXPORT_NAME=wasmModule")
  string(APPEND LINK_FLAGS " -sLLD_REPORT_UNDEFINED")
  string(APPEND LINK_FLAGS " -s DISABLE_EXCEPTION_CATCHING=0")
  message(STATUS "LINK_FLAGS: ${LINK_FLAGS}")
  target_compile_options(<%=config.libName%> PUBLIC "-matomics" "-mbulk-memory")
  set_target_properties(<%=config.libName%> PROPERTIES SUFFIX ".js" LINK_FLAGS "${LINK_FLAGS}")
endif(JS_ONLY)
